package com.hudongwx.godoffood.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.view.InputView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

/**
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_register)
public class RegisterFragment extends Fragment {
    @ViewInject(R.id.reg_user_input)
    private InputView userInput;
    @ViewInject(R.id.reg_pwd_input)
    private InputView pwdInput;
    @ViewInject(R.id.reg_view_see)
    private ImageView seePwdIv;
    private String TAG="RegisterFragment";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = x.view().inject(this, inflater, container);
        pwdInput.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
        return view;
    }
    @Event(R.id.reg_view_see)
    private void lookPwd(View view){
        if(pwdInput.getInputType()== InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD){//明文 如果能看到
            //再次点击  设置成密码看不见  图片闭上眼睛
            pwdInput.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
            seePwdIv.setSelected(false);
        }else {
            // 设置成密码看得见  初始的时候进来，设置为可见，图片页睁开眼
            pwdInput.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            seePwdIv.setSelected(true);
        }
    }

    //第三方登陆
    @Event(R.id.reg_view_qq)
    private void otherLogin(View view) {
        ShareSDK.initSDK(getActivity());
        Platform QQPlatform= ShareSDK.getPlatform(QQ.NAME);
        //因为登录成功以后，就不会再次登录，为了演示，我在登录之前，移除授权
        QQPlatform.removeAccount(true);

        QQPlatform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, final HashMap<String, Object> hashMap) {
                Log.i(TAG, "onComplete: "+hashMap.toString());
                //在ui线程提示
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(),"登录成功："+hashMap.get("nickname"),Toast.LENGTH_SHORT).show();
                        getActivity().finish();
                    }
                });
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.i(TAG, "onError: "+throwable.getMessage());
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Log.i(TAG, "onCancel: "+platform.getName());

            }
        });
//authorize与showUser单独调用一个即可
//        QQPlatform.authorize();//单独授权,OnComplete返回的hashmap是空的
        QQPlatform.showUser(null);//授权并获取用户信息
//移除授权
//weibo.removeAccount(true);
    }
    @Event(R.id.reg_view_weibo)
    private void toWeibo(View view){
        Toast.makeText(getActivity(),"你未安装新浪微博",Toast.LENGTH_SHORT).show();
    }
}
